/**
 * 用户信息数据列表页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航
    activeNavBar("AUTHORITY_USER_INFO", "AUTHORITY");
    //加载数据列表
    loadGrid();
    //重置数据列表宽度
    $("#gridTableWidgetBody").resize(function(){
        $("#gridTable").setGridWidth($(this).width(), false);
    });
    $("#query").click(function(){
        query();
    });
    $("#reset").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.form-select2').each(function(){
            if($(this).prop("disabled") != undefined && $(this).prop("disabled") != true){
                $(this).select2("val", "");
            }
        });
    });
    $("#insert").click(function(){
        openInsertPage();
    });
});

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl+"/web/userInfo/pageQuery.json",
        datatype: "json",
        autowidth: true,
        height: "auto",
        colModel:[
            {name:'id', label:'ID', index:'id', width:50, fixed:false, sortable: false, resizable:false, hidden: true},
            {name:'userPortraitId',label:'用户头像', index:'user_portrait_id', width:100, fixed:false, sortable: false, resize:false, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue != null && cellValue !=""){
                        return '<img src="'+springUrl+'/web/userPortrait/show/'+ cellValue+'" class="no-padding" style="width:100px; height:80px;"/>';
                    }else{
                        return "<div style='height: 80px;'/>";
                    }
                }
            },
            {name:'userCode',label:'用户编码', index:'user_code',width:100, fixed:false, sortable: false, align:'left'},
            {name:'userName',label:'用户名称', index:'user_name', width:100, fixed:false, sortable: false, align:'left'},
            {name:'phone',label:'电话', index:'phone', width:100, fixed:false, sortable: false, align:'left'},
            {name:'email',label:'邮箱', index:'email', width:100, fixed:false, sortable: false, align:'left'},
            {name:'createTime',label:'创建日期', index:'create_date', width:100, fixed:false, sortable: true, align:'center',
                formatter:function(cellValue,options,rowObject){
                    if(cellValue != null){
                        return new Date(cellValue).format("yyyy-MM-dd hh:mm:ss");
                    }else{
                        return "";
                    }
                }
            },
            {name:'', label:'操作', width:250, fixed:true, sortable:false, align:'center', frozen:true,
                formatter:function(cellValue,options,rowObject){
                    var context = "";
                    context += '<button class="btn btn-white btn-info btn-bold" onclick="openAssignRolePage(\''+rowObject.id+'\')"><i class="ace-icon fa fa-users bigger-120 blue"></i>分配角色</button> ';
                    context += '<button class="btn btn-white btn-info btn-bold" onclick="openUpdatePage('+rowObject.id+')"><i class="ace-icon fa fa-pencil bigger-120 blue"></i>修改</button> ';
                    context += '<button class="btn btn-white btn-info btn-bold" onclick="deleteByPrimaryKey('+rowObject.id+')"><i class="ace-icon fa fa-trash-o bigger-120 blue"></i>删除</button>';
                    return context;
                }
            }
        ],
        rownumbers:true,//添加左侧行号
        viewrecords : true,
        rowNum:10,
        rowList:[10,20,30],
        sortname:"create_time",
        sortorder:"desc",
        altRows: true,
        pager : "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "root", //对于json中数据列表
            page: "page",
            total: "total",
            records: "records"
        },
        loadComplete : function() {
            updatePagerIcons(this);
            $(this).setGridWidth($("#gridTableWidgetBody").width(), true);
        }
    });

    //navButtons
    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false,
            addicon : 'ace-icon fa fa-plus-circle purple',
            edit: false,
            editicon : 'ace-icon fa fa-pencil blue',
            del: false,
            delicon : 'ace-icon fa fa-trash-o red',
            search: false,
            searchicon : 'ace-icon fa fa-search orange',
            refresh: false,
            refreshicon : 'ace-icon fa fa-refresh green',
            view: false,
            viewicon : 'ace-icon fa fa-search-plus grey',
        }
    );
}

/**
 * 查询
 */
function query(){
    if($('#serachForm').trigger("validate").isValid()){

        var gridParam = {
            url: springUrl+"/web/userInfo/pageQuery.json",
            postData: {
                userCode : $("#userCode").val(),
                userName : $("#userName").val()
            },
            page:1
        };
        $("#gridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}

/**
 * 打开新增页面
 */
function openInsertPage(){
    window.location.href = springUrl+"/web/userInfo/insertPage";
}

/**
 * 打开修改页面
 * @param id 主键ID
 */
function openUpdatePage(id){
    var element = document.createElement("input");
    element.setAttribute("type", "hidden");
    element.setAttribute("id", "id");
    element.setAttribute("name", "id");
    element.setAttribute("value", id);

    var form = document.createElement("form");
    document.body.appendChild(form);
    form.method = 'post';
    form.action = springUrl+'/web/userInfo/updatePage';
    form.appendChild(element);
    form.submit();
}

/**
 * 打开分配角色页面
 * @param id 主键ID
 */
function openAssignRolePage(id){
    var element = document.createElement("input");
    element.setAttribute("type", "hidden");
    element.setAttribute("id", "id");
    element.setAttribute("name", "id");
    element.setAttribute("value", id);

    var form = document.createElement("form");
    document.body.appendChild(form);
    form.method = 'post';
    form.action = springUrl+'/web/userRoleRelation/assignRolePage';
    form.appendChild(element);
    form.submit();
}

/**
 * 根据主键删除数据
 * @param id 主键ID
 */
function deleteByPrimaryKey(id){
    Commons.showConfirm("是否删除该记录?", function(){
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/userInfo/deleteByPrimaryKey",
            data:{
                id:  id
            },
            success:function (data) {
                Commons.showSuccess("数据删除成功!", function(){
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Commons.showError(data.responseText);
            }
        });
    })
}